package com.carcenter.order.service.impl;

import com.carcenter.order.entity.AppointmentDetail;
import com.carcenter.order.entity.AppointmentMaster;
import com.carcenter.common.pojo.order.MonthSale;
import com.carcenter.order.mapper.AppointmentMasterMapper;
import com.carcenter.order.service.IAppointmentMasterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author car
 * @since 2025-07-16
 */
@Service
public class AppointmentMasterServiceImpl extends ServiceImpl<AppointmentMasterMapper, AppointmentMaster> implements IAppointmentMasterService {

    @Autowired
    AppointmentMasterMapper appointmentMasterMapper;


    @Override
    public MonthSale getMonthAppointmentDetail(Integer storeId, LocalDateTime firstDay, LocalDateTime lastDay) {

        List<AppointmentDetail> appointmentDetails = appointmentMasterMapper.selectMonthAppointmentDetail(storeId, firstDay, lastDay);

        if (appointmentDetails.size() > 0 && appointmentDetails!= null) {
            MonthSale monthSale = new MonthSale();
            monthSale.setStoreId(storeId);
            BigDecimal monthlyRevenue = new BigDecimal("0.0");
            Integer goodsCount = 0;
            Integer serviceCount = 0;
            for (AppointmentDetail appointmentDetail : appointmentDetails) {
                monthlyRevenue=appointmentDetail.getTotalPrice().add(monthlyRevenue);
                if (appointmentDetail.getItemType().equals("产品")){
                    goodsCount++;
                }else {
                    serviceCount++;
                }
            }
            monthSale.setMonthlyRevenue(monthlyRevenue);
            monthSale.setGoodsCount(goodsCount);
            monthSale.setServiceCount(serviceCount);
            return monthSale;
        }else {
            return null;
        }
    }
}
